package com.patrol.logisticsmanage.ui.canteen;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.patrol.logisticsmanage.R;
import com.patrol.logisticsmanage.ui.canteen.bean.PayWay;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PayTypeAdapter extends RecyclerView.Adapter<PayTypeAdapter.ViewHolder> {
    Context context;
    int currentSelect = 0;
    List<PayWay> payWays;

    public PayTypeAdapter(Context context,List<PayWay> payWays) {
        this.context = context;
        this.payWays = payWays;
    }

    public void setCurrentSelect(int currentSelect) {
        this.currentSelect = currentSelect;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_pay_type, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setCurrentSelect(position);
            }
        });
        holder.rbPayType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setCurrentSelect(position);
            }
        });
        if (position == currentSelect) {
            holder.rbPayType.setChecked(true);
        }else {
            holder.rbPayType.setChecked(false);
        }

        holder.rbPayType.setText(payWays.get(position).getTitle());
        if (payWays.get(position).getTitle().equals("微信")){
            holder.imgType.setImageResource(R.drawable.ic_weixinzhifu);
        }else if (payWays.get(position).getTitle().equals("支付宝")){
            holder.imgType.setImageResource(R.drawable.zhifubao);
        }else {
            holder.imgType.setImageResource(R.drawable.ic_guazhangzhifu);
        }
    }

    @Override
    public int getItemCount() {
        return payWays.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.img_type)
        ImageView imgType;
        @BindView(R.id.rb_pay_type)
        RadioButton rbPayType;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
